package com.yan.zktest;



import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.AsyncCallback.StringCallback;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;

public class ZkCreateTest implements Watcher {
	
	private static CountDownLatch countDownLatch = new CountDownLatch(1);
    public static void main(String[] args) throws Exception {
    	ZooKeeper zk = new ZooKeeper("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183",4*1000, new ZkCreateTest());
    	countDownLatch.await();
    	System.out.println("connected, zkState:" + zk.getState());
    	String nodePath1 = zk.create("/rootNode/node1","node1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    	System.out.println("createNode - " + nodePath1);
    	String nodePath2 = zk.create("/rootNode/nodes2","node2".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    	System.out.println("createNode - " + nodePath2);
    	String nodePath3 = zk.create("/rootNode/nodes3","node3".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    	System.out.println("createNode - " + nodePath3);
    	zk.create("/rootNode/nodes4","node4".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, new StringCallback() {
			public void processResult(int state, String nodePath, Object ctx, String nodeName) {
				System.out.println("asynCreateNode - " + nodeName);		
			}
		},"异步传输上下文");
    	
 
    	Thread.sleep(Long.MAX_VALUE);
	}
	
	@Override
	public void process(WatchedEvent event) {
		System.out.println("connectedWatch, event:" + event);
		if (Event.KeeperState.SyncConnected == event.getState()){
			countDownLatch.countDown();
		}

	}
	

	


}
